//UPDATED: 11/22/2010

package com.cs589.smscrypto;
import android.app.Activity;
import android.os.Bundle;

/*
 * This activity class is centered around sending an SMS.
 * In this tab, users will provide a phone number to send the
 * SMS to, as well as the message to be encrypted. All other
 * actions in the encryption process etc. are abstracted
 * from the user.
 */

//imported based on example:
//Reference code on sending SMS: http://goo.gl/OwpnD
import java.util.ArrayList;

import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
//end import

public class SendSMS extends Activity {
    /** Called when the activity is first created. */
	//Declare variables to ease checking of common integers and status codes
    private static final int MAX_SMS_MESSAGE_LENGTH = 160;
    private static final String SMS_DELIVERED = "SMS_DELIVERED";
    private static final String SMS_SENT = "SMS_SENT";
    
    //On the initialization of this class:
	@Override
    public void onCreate(Bundle savedInstanceState) {
    	//Call our xml mockup material
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sendsms);
        
        //get your private key:
        Encrypt test = new Encrypt();
        
        test.getPublic("5056880936");
        //String a = ().toString();
        
        //tv.setText(a);
        
        //Call initializations of send button and notification receivers
        ((Button)findViewById(R.id.send)).setOnClickListener(onButtonClick);
        registerReceiver(sendreceiver, new IntentFilter(SMS_SENT));
        registerReceiver(deliveredreceiver, new IntentFilter(SMS_DELIVERED));
    }
	
	//On exit of activity, call unregister on gathered receivers
	@Override
    protected void onDestroy()
    {
	    unregisterReceiver(sendreceiver);
	    unregisterReceiver(deliveredreceiver);
	    super.onDestroy();
    }

	
    //Listen for a click on the send button
    private View.OnClickListener onButtonClick = new View.OnClickListener()
    {
    	public void onClick(View v)
    	{
    		switch(v.getId())
    		{
				case R.id.send:
				{
			        String phonenumber = ((TextView)findViewById(R.id.contact)).getText().toString();
			        String message = ((TextView)findViewById(R.id.message)).getText().toString();
			        sendSms(phonenumber,message);
			        break;
				}
	        }
	    }
    };

    //Send an SMS message, provide the phone number and message to be sent
    private void sendSms(String phonenumber,String message)
    {
        SmsManager manager = SmsManager.getDefault();
        
        PendingIntent piSend = PendingIntent.getBroadcast(this, 0, new Intent(SMS_SENT), 0);
        PendingIntent piDelivered = PendingIntent.getBroadcast(this, 0, new Intent(SMS_DELIVERED), 0);
        
        
        //encrypt the message to be sent:
        //Encrypt encrypt = new Encrypt();
        //encrypt.encryptMesssage(encrypt.getPublic(phonenumber), message);
        
        
        int length = message.length();
        if(length > MAX_SMS_MESSAGE_LENGTH)
        {
    		//We need to divide the message to 160 char chunks
            ArrayList<String> messagelist = manager.divideMessage(message);
            manager.sendMultipartTextMessage(phonenumber, null, messagelist, null, null);
        }
        else
        {
            manager.sendTextMessage(phonenumber, null, message, piSend, piDelivered);
        }
    }
    //Check of the SMS was delivered
    private BroadcastReceiver deliveredreceiver = new BroadcastReceiver()
    {
        @Override
        public void onReceive(Context context, Intent intent)
        {
            String info = "Delivery information: ";
            
            switch(getResultCode())
            {
                case Activity.RESULT_OK: info += "delivered"; break;
                case Activity.RESULT_CANCELED: info += "not delivered"; break;
            }
            
            Toast.makeText(getBaseContext(), info, Toast.LENGTH_SHORT).show();
        }
    };
    //Check if send was successful
    private BroadcastReceiver sendreceiver = new BroadcastReceiver()
    {
        @Override
        public void onReceive(Context context, Intent intent)
        {
            String info = "Send information: ";
            
            switch(getResultCode())
            {
                case Activity.RESULT_OK: info += "send successful"; break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE: info += "send failed, generic failure"; break;
                case SmsManager.RESULT_ERROR_NO_SERVICE: info += "send failed, no service"; break;
                case SmsManager.RESULT_ERROR_NULL_PDU: info += "send failed, null pdu"; break;
                case SmsManager.RESULT_ERROR_RADIO_OFF: info += "send failed, radio is off"; break;
            }
            Toast.makeText(getBaseContext(), info, Toast.LENGTH_SHORT).show();
        }
    };
    
}